package com.bawei.goods.hospital_goods.service.impl;

import com.bawei.common.utils.PageUtils;
import com.bawei.common.utils.R;
import com.bawei.goods.hospital_goods.es.EsService.EsTService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.bawei.goods.hospital_goods.dao.GoodTableDao;
import com.bawei.goods.hospital_goods.entity.GoodTableEntity;
import com.bawei.goods.hospital_goods.service.GoodTableService;
import com.bawei.common.utils.PageUtils;
import com.bawei.common.utils.Query;

@Service("goodTableService")
public class GoodTableServiceImpl extends ServiceImpl<GoodTableDao, GoodTableEntity> implements GoodTableService {

    @Autowired
    private EsTService esTService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<GoodTableEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)){
            wrapper.like("good_name", key);
        }
        wrapper.eq("good_type_id", params.get("id"));
        wrapper.orderByAsc("sort");


        IPage<GoodTableEntity> page = this.page(
                new Query<GoodTableEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateRow(String id, String row) {
        GoodTableEntity byId = this.getById(id);
        byId.setSort(Integer.parseInt(row));
        this.updateById(byId);
    }

    @Override
    public R grounding(String id, String status) {
        GoodTableEntity byId = this.getById(id);
        byId.setGoodUpStatus(Integer.parseInt(status));
        //存入es中
        esTService.upOrDownGood(status,byId);
        this.baseMapper.updateById(byId);

        return R.ok();
    }

    //同步更新es
    @Override
    public boolean updateById(GoodTableEntity entity) {
        esTService.upOrDownGood("1", entity);
        return super.updateById(entity);
    }
}